<template>
  <el-dropdown trigger="click">
    <div class="avatar">
      <img :src="userStore.userInfo.avatar" alt="avatar" />
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item @click="openDialog('infoRef')">
          <el-icon>
            <User />
          </el-icon>
          {{ $t("header.personalData") }}
        </el-dropdown-item>
        <el-dropdown-item @click="openDialog('passwordRef')">
          <el-icon>
            <Edit />
          </el-icon>
          {{ $t("header.changePassword") }}
        </el-dropdown-item>
        <el-dropdown-item divided @click="logout">
          <el-icon>
            <SwitchButton />
          </el-icon>
          {{ $t("header.logout") }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
  <!-- infoDialog -->
  <!--  <InfoDialog ref="infoRef"></InfoDialog>-->
  <AddDataDialog :columns="tableColumns" ref="addDataDialogRef" :preview="true" title="个人信息"></AddDataDialog>

  <!-- passwordDialog -->
  <PasswordDialog ref="passwordRef"></PasswordDialog>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { LOGIN_URL } from "@/config";
import { useRouter } from "vue-router";
import { useUserStore } from "@/stores/modules/user";
import { ElMessage, ElMessageBox } from "element-plus";
import PasswordDialog from "./PasswordDialog.vue";
import { loginController } from "@/api/modules/dwh/user";
import AddDataDialog from "@/views/dwh/tableColumn/addDataDialog.vue";
import { API } from "@/api/modules/dwh/typings";
import { tableColumnController } from "@/api/modules/dwh";
import { DimensionTypeEnum } from "@/utils/dict";
import { camelToUnderline } from "@/utils";

const router = useRouter();
const userStore = useUserStore();

// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    // 1.执行退出登录接口
    await loginController.logout();

    // 2.清除 Token
    userStore.setToken("");

    // 3.重定向到登陆页
    router.replace(LOGIN_URL);
    ElMessage.success("退出登录成功！");
  });
};

// 打开修改密码和个人信息弹窗
// const infoRef = ref<InstanceType<typeof InfoDialog> | null>(null);
const addDataDialogRef = ref<InstanceType<typeof AddDataDialog> | null>(null);
const passwordRef = ref<InstanceType<typeof PasswordDialog> | null>(null);
const openDialog = (ref: string) => {
  if (ref == "infoRef") {
    let userInfo = userStore.userInfo;
    let reduce = Object.keys(userInfo).reduce((accumulator: { [key: string]: any }, currentValue) => {
      // 设置初始值为空字符串或其他默认值
      accumulator[camelToUnderline(currentValue)] = userInfo[currentValue];
      return accumulator;
    }, {});
    addDataDialogRef.value?.show(reduce, true);
  }
  if (ref == "passwordRef") passwordRef.value?.openDialog();
};
onMounted(() => {
  // 加载表格列
  const tableId = import.meta.env.VITE_USER_TABLE_ID;
  listTableColumns(tableId);
});

let tableColumns = ref<Array<API.TableColumnVo>>([]);

const listTableColumns = async (id: string | undefined) => {
  let result = await tableColumnController.listUsingGET({ param: Number(id) });
  result.data.forEach(data => {
    if (data.dimensionType == DimensionTypeEnum.agg) {
      data.columnCode = data.columnCode.split("@")[1];
    }
    data.dimensionType = [data.dimensionType, data.dimensionExtra];
  });
  tableColumns.value = result.data;
};
</script>

<style scoped lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  overflow: hidden;
  cursor: pointer;
  border-radius: 50%;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>
